package com.logicwin.aibala.ui.screens.screenmain;

import java.util.List;

import com.logicwin.aibala.R;
import com.logicwin.aibala.detailPackActivity.testActivity;
import com.logicwin.aibala.model.androidSWInfo.APKListItem;
import com.logicwin.aibala.model.androidSWInfo.AndroidSWInfoService;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;

public class ApkAdapterHome extends ApkPackageAdapter{	
	
	public ApkAdapterHome(Activity activity, List<APKListItem> apkList) {
		super(activity, apkList);
		init();
		// TODO Auto-generated constructor stub
	}
	private void init(){
		if(this.getCurrApkList() == null)
			this.setCurrApkList(AndroidSWInfoService.getHotList(AndroidSWInfoService.CLASS_ID_GAME));		
	}
	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		if(position == 0) return null;
		return super.getItem(position-1);
		//return null;
	}
	public View getView(int position, View convertView, ViewGroup parent) {

		View view = convertView;
		APKListItem item;
		
		if(position == 0) {
			view = this.getParentActivity().getLayoutInflater().inflate(R.layout.ad, null);
			return view;
		}		
		else 
		{	
			view = this.getParentActivity().getLayoutInflater().inflate(R.layout.package_item, null);
			if(position <= 0) return view;
			
			if ((item = this.getCurrApkList().get(position - 1)) == null) {
				return view;
			}			
			inflateItemView(view, item);
			Button btnDownload = (Button) view.findViewById(R.id.download);
			btnDownload.setOnClickListener(new ListViewItemButtonClickListener(item)
			);			
			return view;	
		}
	}
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if(position == 0) return;
		// TODO Auto-generated method stub
		Log.i("ApkPackageAdapter", getCurrApkList().get(position-1).getAPKName());
		Intent intent = new Intent(mParentActivity, testActivity.class);
		
		intent.putExtra("resId", mApkList.get(position-1).getDetailResID());
		mParentActivity.startActivity(intent);		
		
	}	
}
